package com.jule.miaozhuapc.controller;

import com.jule.miaozhuapc.ano.Auth;
import com.jule.miaozhuapc.ano.TokenUser;
import com.jule.miaozhuapc.domain.WawaUser;
import com.jule.miaozhuapc.dto.WsUtil;
import com.jule.miaozhuapc.service.PingPayService;
import com.jule.miaozhuapc.util.Const;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping( value = Const.WAWA_HTTP_URL_V1+ "/ping")
public class PingController {

	@Autowired
	PingPayService pingPayService;

	private Logger logger = Logger.getLogger(PingController.class);

	@Auth
	@RequestMapping(value="/createCharge",method=RequestMethod.GET, produces = "application/json; charset=utf-8")
	public @ResponseBody
	String CreateCharge(@RequestParam(name="payConfigId") String payConfigId,
			@RequestParam(name="channel") String channel,
			@TokenUser WawaUser user) throws JSONException {

		JSONObject rst = new JSONObject();
		JSONObject status = new JSONObject();

		// https://www.likewawa.com/miaozhuapc/v1/ping/createCharge?payConfigId=1&channel=
		WsUtil ws = new WsUtil();
		if ("".equals(payConfigId) || "".equals(channel)){
			status.put("code", "00000001");
			status.put("message", "error");
			return rst.put("status", status).toString();
		}
		JSONObject obj = pingPayService.createCharge(user, Long.parseLong(payConfigId), channel);

		logger.info(obj);
		status.put("code", "00000000");
		status.put("message", "success");
		rst.put("status", status);
		rst.put("body", obj);
		logger.info(rst);
		return rst.toString();
	}

	public static  void main(String[] args){

	}
}
